﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace Archiver
{
    class FileReader
    {

        private List<bool> BitsFromFile;
        private int kolvo;
        public FileReader(string FileName)
        {
            Console.Write("Запись файла в память:");
            var Time = DateTime.Now;
            if (!File.Exists(FileName))
                throw new FileNotFoundException("Файл не найден!", FileName);
            var f = File.Open(FileName, FileMode.Open);
            BitsFromFile = new List<bool>();
            
            var i = 0;
            while (f.Position<f.Length)
            {
                var b = f.ReadByte();
                var bin = Binary.ConvertIntToBin(b, 8);
                for (var j = 0; j < 8; j++)
                {
                    BitsFromFile.Add(bin[j] != '0');
                    i++; 
                }
            }
            f.Close();
            kolvo = 0;
            var tt = DateTime.Now - Time; 
            Console.WriteLine(tt.Seconds+","+tt.Milliseconds+" сек.");
            Console.WriteLine("Начало архивации: ");
        }

        public List<bool> GetBits(int n)
        {
            if (Length < n)
                n = Length;

            var res = new List<bool>();
            for (var i = 0; i < n; i++)
                res.Add(BitsFromFile[i+kolvo]);
            return res; 
        }

        public void DeleteBits(int n)
        {
            if (Length<+n)
                this.kolvo += Length;
            else
                this.kolvo += n;
        }

        public int Length
        {
            get { return BitsFromFile.Count-kolvo; }
        }
    }
}
